package ui.swing.tabbedpane;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TabValidationDemo extends JFrame {

	private static final long serialVersionUID = -2954730519742036557L;

	public static final int LIST_TAB = 0;

	public static final int DATA_TAB = 1;
	
	protected Person[] m_employees = new Person[] {
			new Person("John", "Smith", "111-1111"),
			new Person("Silvia", "Glenn", "222-2222"),
			new Person("Captain", "Kirk", "333-3333"),
			new Person("Duke", "Nukem", "444-4444"),
			new Person("James", "Bond", "000-7777") 
	};
	
	protected JList m_list;
	protected JTextField m_firstTxt;
	protected JTextField m_lastTxt;
	protected JTextField m_phoneTxt;
	
	protected JTabbedPane m_tab;
	
	public TabValidationDemo() {
		super("Tab Validation Demo");
		
		JPanel p1 = new JPanel(new BorderLayout());
		p1.setBorder(new EmptyBorder(10, 10, 10, 10));

		m_list = new JList(m_employees);
		m_list.setVisibleRowCount(4);
		
		JScrollPane sp = new JScrollPane(m_list);
		p1.add(sp, BorderLayout.CENTER);

		MouseListener mlst = new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2)
					m_tab.setSelectedIndex(DATA_TAB);
			}
		};
		m_list.addMouseListener(mlst);
		
		JPanel p2 = new JPanel(new GridLayout(3,2));
		p2.setBorder(new EmptyBorder(10, 10, 10, 10));
		p2.add(new JLabel("First name:"));
		m_firstTxt = new JTextField(20);
		p2.add(m_firstTxt);
		p2.add(new JLabel("Last name:"));
		m_lastTxt = new JTextField(20);
		p2.add(m_lastTxt);
		p2.add(new JLabel("Contact phone:"));
		m_phoneTxt = new JTextField(20);
		p2.add(m_phoneTxt);
		
		m_tab = new JTabbedPane();
		m_tab.addTab("Employees", p1);
		m_tab.addTab("Personal Data", p2);
		m_tab.addChangeListener(new TabChangeListener());
		
		JPanel p = new JPanel();
		p.add(m_tab);
		p.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(p);
		pack();
	}
	
	class TabChangeListener implements ChangeListener {
		public void stateChanged(ChangeEvent e) {
			Person sp = (Person)m_list.getSelectedValue();
			switch(m_tab.getSelectedIndex())
			{
			case DATA_TAB:
				if(sp == null){
					m_tab.setSelectedIndex(LIST_TAB);
					return;
				}
				m_firstTxt.setText(sp.m_firstName);
				m_lastTxt.setText(sp.m_lastName);
				m_phoneTxt.setText(sp.m_phone);
				break;
			case LIST_TAB:
				if(sp!=null) {
					sp.m_firstName = m_firstTxt.getText();
					sp.m_lastName = m_lastTxt.getText();
					sp.m_phone = m_phoneTxt.getText();
					m_list.repaint();
				}
				break;
			}
		}
	}
	
	public static void main(String[] args) {
		JFrame frame = new TabValidationDemo();
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}

class Person {

	public String m_firstName;

	public String m_lastName;

	public String m_phone;

	public Person(String firstName, String lastName, String phone) {
		m_firstName = firstName;
		m_lastName = lastName;
		m_phone = phone;
	}

	public String toString() {
		String str = m_firstName + " " + m_lastName;
		if (m_phone.length() > 0)
			str += "(" + m_phone + ")";
		return str.trim();
	}
}
	